﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using saigonbook.Biz;
using hethongbansach.Models;

namespace hethongbansach.Controllers
{
    public class nguoidungController : Controller
    {
        public List<BIZ_user> danhsachnguoidung = new List<BIZ_user>();
        public BIZ_user dbnguoidung = new BIZ_user();

        public void dsnguoidung()
        {
            danhsachnguoidung.Clear();
            danhsachnguoidung = dbnguoidung.Select_user();
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult DangNhap()
        {
            return View();
        }

        public bool kiemtradangnhap(BIZ_user nd)
        {
            dsnguoidung();
            foreach (BIZ_user item in danhsachnguoidung)
            {
                if (item.username.Equals(nd.username) && item.password.Equals(nd.password))
                    return true;
            }
            return false;
        }

        [HttpPost]
        public ActionResult DangNhap(dangnhap model, string returnUrl)
        {
            try
            {
                BIZ_user nd = new BIZ_user();
                nd.username = model.username;
                nd.password = model.password;
                bool _remember = model.remember;
                bool userValid = kiemtradangnhap(nd);
                if (userValid)
                {
                    Session["user"] = nd.username;
                    FormsAuthentication.SetAuthCookie(nd.username, _remember);
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("TrangChu", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Sai tên hoặc mật khẩu đăng nhập");
                    return View();
                }
            }
            catch
            {
                return View();
            }
        }

        public ActionResult SuaThongTin()
        {
            if (Session["user"] == null)
            {
                return View();
            }
            else
            {
                string username = Session["user"].ToString();
                dsnguoidung();
                BIZ_user nd = new BIZ_user();
                foreach (BIZ_user item in danhsachnguoidung)
                    if (item.username.Equals(username))
                    {
                        nd = item;
                        break;
                    }
                suathongtin _sua = new suathongtin();
                _sua.username = nd.username;
                _sua.hoten = nd.hoten;
                _sua.sdt = nd.sodienthoai;
                _sua.email = nd.email;
                _sua.password = null;
                _sua.passwordcu = null;
                _sua.xacnhanmatkhau = null;
                return View(_sua);
            }
        }

        [HttpPost]
        public ActionResult SuaThongTin(suathongtin nd_sua)
        {
            BIZ_user _nd = new BIZ_user();
            _nd.username = nd_sua.username;
            _nd.password = nd_sua.passwordcu;
            if (Session["user"].Equals(_nd.username))
            {
                if (nd_sua.password == null || !nd_sua.password.Equals(nd_sua.xacnhanmatkhau))
                {
                    ViewBag.saithongtin = "";
                    return View(nd_sua);
                }
                else
                {
                    bool userValid = kiemtradangnhap(_nd);
                    if (userValid)
                    {
                        dsnguoidung();
                        foreach (BIZ_user item in danhsachnguoidung)
                            if (item.username.Equals(nd_sua.username))
                            {
                                item.password = nd_sua.password;
                                item.sodienthoai = nd_sua.sdt;
                                item.email = nd_sua.email;
                                break;
                            }
                        foreach(BIZ_user u in danhsachnguoidung)
                            if (u.username == nd_sua.username)
                            {
                                _nd = u;
                                break;
                            }
                        _nd.Sua_user(_nd);
                        return RedirectToAction("TrangChu", "Home");
                    }
                    else
                    {
                        ViewBag.saithongtin = "Sai mật khẩu cũ";
                        return View(nd_sua);
                    }
                    
                }
            }
            else
            {
                return RedirectToAction("TrangChu", "Home");
            }
        }

        public ActionResult DangXuat()
        {
            Session["user"] = null;
            FormsAuthentication.SignOut();
            return RedirectToAction("TrangChu", "Home");
        }

        public bool kiemtranguoidung(BIZ_user nd)
        {
            dsnguoidung();
            foreach (BIZ_user item in danhsachnguoidung)
            {
                if (item.username.Equals(nd.username))
                    return false;
            }
            return true;
        }

        bool kiemtranamnhuan(int nam)
        {
            if (nam % 4 == 0 && nam % 100 != 0)
                return true;
            return false;
        }

        bool kiemtrangaysinh(int ngay, int thang, int nam)
        {
            int ngaynhuan = 0;
            if (thang == 2)
            {
                if (kiemtranamnhuan(nam))
                    ngaynhuan = 29;
                else
                    ngaynhuan = 28;
                if (ngay > ngaynhuan)
                {
                    return false;
                }
            }
            else
                if (thang == 4 || thang == 6 || thang == 9 || thang == 11)
                    if (ngay > 30)
                        return false;
            return true;
        }

        public void ngaythangnam()
        {
            int namtu = DateTime.Now.Year - 65;
            int namden = DateTime.Now.Year - 16;
            List<SelectListItem> ngayList = new List<SelectListItem>();
            List<SelectListItem> thangList = new List<SelectListItem>();
            List<SelectListItem> namList = new List<SelectListItem>();
            for (int i = 1; i <= 31; i++)
            {
                string j = i.ToString();
                ngayList.Add(new SelectListItem { Text = j, Value = j });
            }
            for (int i = 1; i <= 12; i++)
            {
                string j = i.ToString();
                thangList.Add(new SelectListItem { Text = j, Value = j });
            }
            for (int i = namtu; i <= namden; i++)
            {
                string j = i.ToString();
                namList.Add(new SelectListItem { Text = j, Value = j });
            }
            ViewBag.ngay = ngayList;
            ViewBag.thang = thangList;
            ViewBag.nam = namList;
        }

        [HttpGet]
        public ActionResult DangKy()
        {
            ngaythangnam();
            return View();
        }

        [HttpPost]
        public ActionResult DangKy(dangky model, FormCollection form)
        {
            int gt = int.Parse(form["gioitinh"].ToString());
            int ngay = int.Parse(form["ngay"]);
            int thang = int.Parse(form["thang"]);
            int nam = int.Parse(form["nam"]);
            if (kiemtrangaysinh(ngay, thang, nam))
            {
                DateTime ns = new DateTime(nam, thang, ngay);
                try
                {
                    BIZ_user nd = new BIZ_user();
                    nd.username = model.username;
                    nd.password = model.password;
                    nd.hoten = model.hoten;
                    nd.ngaysinh = ns;
                    nd.sodienthoai = model.sdt;
                    nd.diachi = model.diachi;
                    nd.email = model.email;
                    nd.gioitinh = gt;
                    nd.phanquyen = new BIZ_phanquyen();
                    nd.phanquyen.maphanquyen = 1;
                    if (kiemtranguoidung(nd))
                    {
                        dbnguoidung.Them_user(nd);
                        danhsachnguoidung.Add(nd);
                        ViewBag.thanhcong = "Đăng ký thành công!";
                    }
                    else
                    {
                        ngaythangnam();
                        ViewBag.thanhcong = null;
                    }
                    ViewBag.thatbai = "Tên đăng nhập đã tồn tại!";
                    ngaythangnam();
                    return View();
                }
                catch
                {
                    ngaythangnam();
                    return View();
                }
            }
            else
            {
                ngaythangnam();
                ViewBag.loi = "Ngày sinh không hợp lệ!";
            }
            ngaythangnam();
            return View();
        }
    }
}
